﻿using Newtonsoft.Json;

using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SMSSendTest
{
    public class UMCSmsService
    {
        // private static readonly string apiUrl = "http://106.74.72.240:8000/umcinterface/sendtempletmsg";


        private static string GetMd5Hash(string input)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                // 计算字符串的哈希值  
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                // 创建一个StringBuilder来收集字节  
                // 并创建字符串（十六进制格式）  
                StringBuilder sBuilder = new StringBuilder();

                // 循环遍历哈希数据的每个字节  
                // 并格式化为十六进制字符串  
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }

                // 返回十六进制字符串  
                return sBuilder.ToString();
            }
        }

        public static string SendUmcSmsAsync(string phoneNumber, string message)
        {
            try
            {
                var apiUrl = "http://rcsapi.wo.cn:8000/umcinterface/sendtempletmsg";
                var data = new
                {
                   key = "d7063fcc2f1857868b8892423171a056",
                    cpcode = "AAAYHG",
                    msg = message,
                    mobiles = phoneNumber,
                    excode = "",
                    templetid = "198600"
                };


                var json = new
                {
                  
                    cpcode = data.cpcode,
                    msg = data.msg,
                    mobiles = phoneNumber,
                    excode = data.excode,
                    templetid = data.templetid,
                    sign = GetMd5Hash(data.cpcode + data.msg + data.mobiles + data.excode + data.templetid + data.key)
                };
                var parameters = new Dictionary<string, string>();

                parameters.Add("cpcode", data.cpcode);

                parameters.Add("msg", data.msg);

                parameters.Add("mobiles", phoneNumber);

                parameters.Add("excode", data.excode);
                parameters.Add("templetid", data.templetid);
                parameters.Add("sign", GetMd5Hash(data.cpcode + data.msg + data.mobiles + data.excode + data.templetid + data.key));



                string result1 = null;// WebRequestHelper.sendPost(apiUrl, parameters, "post");

                return result1; // 这里返回的可能是发送成功的消息或者短信ID等   

            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");

            }
            return null;
        }
        public static string SendUmcJSONSmsAsync(string phoneNumber, string message)
        {
            try
            {
                var apiUrl = "http://rcsapi.wo.cn:8000/umcinterface/sendtempletmsg";
                var data = new
                {
                    key = "d7063fcc2f1857868b8892423171a056",
                    cpcode = "AAAYHG",
                    msg = message,
                    mobiles = phoneNumber,
                    excode = "",
                    templetid = "198600"
                };


                var json = new
                {
                 //   key = data.key,
                    cpcode = data.cpcode,
                    msg = data.msg,
                    mobiles = phoneNumber,
                    excode = data.excode,
                    templetid = data.templetid,
                    sign = GetMd5Hash(data.cpcode + data.msg + data.mobiles + data.excode + data.templetid + data.key)
                };

                string result1 = null;//WebRequestHelper.HttpJsonPost(apiUrl, json);

                return result1; // 这里返回的可能是发送成功的消息或者短信ID等   

            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");

            }
            return null;
        }

        public static string SendUmcJSONSms2(string phoneNumber, string message)
        {
            try
            {
                var apiUrl = "http://rcsapi.wo.cn:8000";
                var data = new
                {
                    key = "d7063fcc2f1857868b8892423171a056",
                    cpcode = "AAAYHG",
                    msg = "23222333,22",
                    mobiles = phoneNumber,
                    excode = "",
                    templetid = "198597"

                };


                var client = new RestClient(apiUrl);

                var request = new RestRequest("/umcinterface/sendtempletmsg", Method.Post);
                  request.AddHeader("Content-Type", "application/json");
                // request.AddParameter("application/json", requestBody, ParameterType.RequestBody);
                request.AddJsonBody(new
                {
               //     key = data.key,
                    cpcode = data.cpcode,
                    msg = data.msg,
                    mobiles = phoneNumber,
                    excode = data.excode,
                    templetid = data.templetid,
                    sign = GetMd5Hash(data.cpcode + data.msg + data.mobiles + data.excode + data.templetid + data.key)
                });

                var reSend = client.Execute(request);
                  
                return reSend.Content; // 这里返回的可能是发送成功的消息或者短信ID等   

            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");

            }
            return null;
        }
        public static string SendJuheIpAsync(string phoneNumber, string message)
        {
            try
            {
                var apiUrl = "http://apis.juhe.cn/ip/ipNewV3";

                var json = new
                {
                    ip = "122.193.105.215",
                    key = "eef34837470d30aa82350a85c6c65fe2"

                };

                //var result1 = WebRequestHelper.HttpJsonPost(apiUrl, json);

                var parameters = new Dictionary<string, string>();
                parameters.Add("ip", "122.193.105.215");
                parameters.Add("key", "eef34837470d30aa82350a85c6c65fe2");


                string result1 = null;// = null;//WebRequestHelper.sendPost(apiUrl, parameters, "post");

                //   var result1 = WebRequestHelper.HttpJsonPost(apiUrl, json);

                Console.WriteLine(result1);
                return result1; // 这里返回的可能是发送成功的消息或者短信ID等   

            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");

            }
            return null;
        }

        public static string SendUMS86Async(string phoneNumber, string message)
        {
            try
            {
                var apiUrl = "http://api.ums86.com:8899/sms/Api/Send.do";

                var json = new
                {
                    ip = "122.193.105.215",
                    key = "eef34837470d30aa82350a85c6c65fe2"

                };

                //var result1 = WebRequestHelper.HttpJsonPost(apiUrl, json);

                var parameters = new Dictionary<string, string>();
                parameters.Add("ip", "122.193.105.215");
                parameters.Add("key", "eef34837470d30aa82350a85c6c65fe2");


                // var result1 = WebRequestHelper.sendPost(apiUrl, parameters, "get");

                string result1 = null;// WebRequestHelper.HttpJsonPost(apiUrl, json);

                Console.WriteLine(result1);
                return result1; // 这里返回的可能是发送成功的消息或者短信ID等   

            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");

            }
            return null;
        }

        public static string SendAmapWeatherInfo(string phoneNumber, string message)
        {
            try
            {
                var apiUrl = "https://restapi.amap.com/v3/weather/weatherInfo";

                var json = new
                {
                    ip = "122.193.105.215",
                    key = "eef34837470d30aa82350a85c6c65fe2"

                };

                //var result1 = WebRequestHelper.HttpJsonPost(apiUrl, json);

                var parameters = new Dictionary<string, string>();
                parameters.Add("city", "110101");
                parameters.Add("key", "b5edbfa327e43756cb65711bb10d409d");


                // var result1 = WebRequestHelper.sendPost(apiUrl, parameters, "get");

                string result1 = null;// WebRequestHelper.sendPost(apiUrl, parameters,"get");

                Console.WriteLine(result1);
                return result1; // 这里返回的可能是发送成功的消息或者短信ID等   

            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");

            }
            return null;
        }

      
    }
}
